/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import java.util.HashSet;

import org.openaecfm.model.kernel.Context;
import org.openaecfm.model.kernel.Root;
import org.openaecfm.model.measure.UnitAssignment;
import org.openaecfm.model.representation.RepresentationContext;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ContextIO implements IEntityInstanceUtilOutput<Context>,
		IEntityInstanceUtilInput<Context> {

	private IEntityInstanceUtilOutput<Root> utilOutput;
	private IEntityInstanceUtilInput<Root> utilInput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Root> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<Root> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, Context pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ObjectType
		Parameter objectType = sei.getParameter(4);
		if (objectType.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) objectType;
			pojo.setObjectType(s.toUTF8());
		}

		// Set LongName
		Parameter longName = sei.getParameter(5);
		if (longName.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) longName;
			pojo.setLongName(s.toUTF8());
		}

		// Set Phase
		Parameter phase = sei.getParameter(6);
		if (phase.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) phase;
			pojo.setPhase(s.toUTF8());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, Context pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set RepresentationContexts
		Parameter representationContexts = sei.getParameter(7);
		if (representationContexts.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) representationContexts;
			if (pojo.getRepresentationContexts() == null) {
				pojo.setRepresentationContexts(new HashSet<RepresentationContext>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getRepresentationContexts().add(
						(RepresentationContext) appDataManager.getObject(e
								.toInstanceName()));
			}
		}

		// Set UnitsInContext
		Parameter unitsInContext = sei.getParameter(8);
		if (unitsInContext.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) unitsInContext;
			pojo.setUnitsInContext((UnitAssignment) appDataManager.getObject(e
					.toInstanceName()));
		}
	}

	@Override
	public void exportSPF(Context pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ObjectType
		if (pojo.getObjectType() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getObjectType(),
					StringParameter.UTF_8));
		}

		// Set LongName
		if (pojo.getLongName() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getLongName(),
					StringParameter.UTF_8));
		}

		// Set Phase
		if (pojo.getPhase() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getPhase(),
					StringParameter.UTF_8));
		}
	}
}
